<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-12-28 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

function select($name, $options, $default=null, $extra=null)
{
	if($default === 0)settype($default,'string');
	
	$size = '';
	if(stristr($extra,'multiple') !== false && stristr($extra,'size') === false)
	{
		$size = 'size="'.count($options).'"';
	}
	
	$string = "<select name=\"{$name}\" id=\"{$name}\" {$size} class='select' $extra>\r\n";
	$selected = '';
	
	foreach($options as $label => $option)
	{
		$option['value'] = isset($option['value']) ? $option['value'] : $label;
		if(isset($option['name']))
		{
			if($default !== null)
			{
				$selected = in_array($option['value'],(array)$default) ? 'selected' : '';
			}
			
			$option_extra = isset($option['extra']) ? $option['extra'] : '';
			
			$string .= "\t<option value='{$option['value']}' {$selected} {$option_extra}>{$option['name']}</option>\r\n";
		}
		else
		{
			$string .= "<optgroup label={$label}>";
			
			foreach ($option as $opt)
			{
				if(is_array($opt) && $opt['name'] && $opt['value'])
				{
					if($default !== null)
					{
						$selected = in_array($opt['value'],(array)$default) ? 'selected' : '';
					}
					
					$opt_extra = isset($opt['extra']) ? $opt['extra'] : '';
					
					$string.="\t<option value='{$opt['value']}' {$selected} {$opt_extra}>{$opt['name']}</option>\r\n";
				}
			}
			$string.="</optgroup>";
		}
	}
	
	$string.="</select>\r\n";
	
	return $string;
}

function page($total_record, $per_page_num, $current_page, $url='', $per_page_nums='')
{
	$html = '';
	if($total_record > $per_page_num)
	{
		//$halfper = (int) ($config['page_display'] / 2);
		$halfper = 2;
		$char = '&nbsp;&nbsp;';
		$extra = '';
		$var = 'page';
		
		$p_val = "V01001page10010V";
		$url = ('' == $url) ? "?$var={$p_val}" : (($url_no_page = (false !== strpos($url,"&{$var}=") ? preg_replace("/\&?{$var}\=[^\&]*/i",'',$url) : $url))."&{$var}={$p_val}");
		
		$url_postfix = '';
		$total_page = ceil($total_record / $per_page_num);

		//$html = ($current_page - 1 >= 1) ? "\n<a href='{$url_no_page}{$url_postfix}' title=1 {$extra}>首页</a>{$char}\n<a href='".(1 == ($previous_page = ($current_page - 1)) ? $url_no_page : str_replace($p_val,(true===$idencode?idencode($previous_page):$previous_page),$url))."{$url_postfix}' title=$previous_page {$extra}>上一页</a>{$char}" : "首页{$char}上一页{$char}";
		if($current_page - 1 >= 1)
		{
			$html = "\n<a href='{$url_no_page}{$url_postfix}' title=1 {$extra}>首页</a>{$char}\n<a href='".
					(1 == ($previous_page = ($current_page - 1)) ? $url_no_page : str_replace($p_val, $previous_page, $url)).
					"{$url_postfix}' title=$previous_page {$extra}>上一页</a>{$char}";
		}
		else
		{
			$html = "首页{$char}上一页{$char}";
		}

		for($i=$current_page-$halfper,$i>0 or $i=1,$j=$current_page + $halfper,$j<$total_page or $j=$total_page; $i<=$j; $i++)
		{
			$html .= ($i == $current_page) ? "\n<B>".($i)."</B>{$char}" : "\n<a href='".(1 == $i ? $url_no_page : str_replace($p_val, $i, $url))."{$url_postfix}' title=$i {$extra}>".($i)."</a>{$char}";
		}

		$html .= (($next_page = ($current_page + 1)) > $total_page) ? "下一页{$char}尾页" : "\n<a href='".str_replace($p_val, $next_page, $url)."{$url_postfix}' title=$next_page {$extra}>下一页</a>{$char}\n<a href='".str_replace($p_val, $total_page, $url)."{$url_postfix}' title=$total_page {$extra}>尾页</a>";

		if(!empty($per_page_nums))
		{
			$per_page_num_list = is_array($per_page_nums) ? $per_page_nums : explode(" ",$per_page_nums);
			$current_url = str_replace($p_val, $current_page, $url).$url_postfix;
			$pn_postfix = "&count=";
			$per_page_num_select = "<select name='count' onchange=\"window.location='{$current_url}{$pn_postfix}'+this.value\">";
			foreach ($per_page_num_list as $_per_page_num)
			{
				$selected = ($_per_page_num == $per_page_num) ? "selected" : "";
				$per_page_num_select .= "<option value={$_per_page_num} $selected>{$_per_page_num}";
			}
			$per_page_num_select .= "</select>";
		}
		else
		{
			$per_page_num_select = "<i>{$per_page_num}</i>";
		}

		$html ="<div id='page'> 当前<i>{$current_page}</i>/共<i>{$total_page}</i>页 {$html} 每页显示${per_page_num_select}条/共<i>{$total_record}</i>条</div>";
	}

	return $html;
}